﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using HYElectricCalculation;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class LoadCalculationForms : System.Windows.Forms.Form
	{
		public LoadCalculationForms(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			this.cmdData = cmdData;
			this.ui_doc = cmdData.Application.ActiveUIDocument.Document;
		}

		private void tsbExsit_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
			}
			base.Close();
		}

		private void tsbCreateItem_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
			}
			this.loadDatagridView();
			this.isEdit = false;
		}

		private void tsbProjectAdd_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
			}
			this.loadDatagridView();
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].ReadOnly = false;
			List<StatisticsData> list = StatisticsItem.CountEquipment(this.ui_doc);
			for (int i = 0; i < list.Count; i++)
			{
				StatisticsData statisticsData = new StatisticsData();
				statisticsData = list[i];
				this.dataGridView1.Rows[i].Cells[0].Value = i + 1;
				this.dataGridView1.Rows[i].Cells[1].Value = statisticsData.CatagoryName;
				if (statisticsData.Phase == "1")
				{
					this.dataGridView1.Rows[i].Cells[2].Value = "A";
					this.dataGridView1.Rows[i].Cells[3].Value = 0.22;
				}
				else
				{
					this.dataGridView1.Rows[i].Cells[2].Value = "A,B,C";
					this.dataGridView1.Rows[i].Cells[3].Value = 0.38;
				}
				this.dataGridView1.Rows[i].Cells[4].Value = Math.Round(statisticsData.EquipPower / 1000.0, 2);
				this.dataGridView1.Rows[i].Cells[5].Value = statisticsData.NeedFactor;
				this.dataGridView1.Rows[i].Cells[6].Value = statisticsData.PowerFactor;
				this.TotalCalculationForm(i);
				this.dataGridView1.Rows.Add();
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[5].Value = 1;
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[6].Value = 0.8;
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[2].Value = "A,B,C";
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[3].Value = 0.38;
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[7].Style.BackColor = this.color;
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[8].Style.BackColor = this.color;
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[9].Style.BackColor = this.color;
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[10].Style.BackColor = this.color;
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].DefaultCellStyle.BackColor = System.Drawing.Color.White;
			}
			for (int j = 0; j < this.dataGridView1.Columns.Count; j++)
			{
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].Cells[j].Value = "";
			}
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].ReadOnly = true;
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].DefaultCellStyle.BackColor = this.color;
			this.isEdit = (list.Count > 0);
		}

		private void tsbLoad_Click(object sender, EventArgs e)
		{
			if (this.isEdit)
			{
				MessageBoxButtons buttons = MessageBoxButtons.YesNo;
				if (MessageBox.Show("是否保存当前数据？", "盈建科提示", buttons) == DialogResult.Yes)
				{
					this.SaveXml();
				}
			}
			this.openFileDialog1.Filter = "XLH文件(*.xlh)|*.xlh";
			this.openFileDialog1.FileName = "";
			if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				string fileName = this.openFileDialog1.FileName;
				this.fullPath = fileName;
				DataSet dataSet = new DataSet();
				dataSet.ReadXml(fileName);
				DataTable dataTable = dataSet.Tables[0];
				dataTable.Rows.Remove(dataTable.Rows[dataTable.Rows.Count - 2]);
				this.GetTablToDgv(this.dataGridView1, dataTable);
				this.GetTablToDgv(this.dgv, dataSet.Tables[1]);
				this.GetTablToDgv(this.dataGridView3, dataSet.Tables[2]);
				this.isEdit = false;
			}
			if (this.dataGridView1.RowCount > 3)
			{
				this.TotalCalculationForm(0);
			}
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].DefaultCellStyle.BackColor = System.Drawing.Color.White;
			for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
			{
				this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].Cells[i].Value = "";
			}
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].ReadOnly = true;
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].ReadOnly = true;
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].DefaultCellStyle.BackColor = this.color;
		}

		public void GetTablToDgv(DataGridView dataGridView1, DataTable dt)
		{
			dataGridView1.RefreshEdit();
			dataGridView1.Rows.Clear();
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				dataGridView1.Rows.Add();
				for (int j = 0; j < dt.Columns.Count; j++)
				{
					if (i == dt.Rows.Count - 2)
					{
						dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[j].Value = dt.Rows[dt.Rows.Count - 1][j].ToString();
					}
					else
					{
						dataGridView1.Rows[i].Cells[j].Value = dt.Rows[i][j].ToString();
					}
				}
			}
		}

		public DataTable GetDgvToTable(DataGridView dgv)
		{
			DataTable dataTable = new DataTable();
			for (int i = 0; i < dgv.Columns.Count; i++)
			{
				DataColumn column = new DataColumn(dgv.Columns[i].Name.ToString());
				dataTable.Columns.Add(column);
			}
			for (int j = 0; j < dgv.Rows.Count; j++)
			{
				DataRow dataRow = dataTable.NewRow();
				for (int k = 0; k < dgv.Columns.Count; k++)
				{
					if (dgv.Rows[j].Cells[k].Value == null)
					{
						dataRow[k] = "";
					}
					else
					{
						dataRow[k] = dgv.Rows[j].Cells[k].Value.ToString();
					}
				}
				dataTable.Rows.Add(dataRow);
			}
			return dataTable;
		}

		private void tsbSave_Click(object sender, EventArgs e)
		{
			this.SaveXml();
		}

		public void SaveXml()
		{
			string fileName = this.saveFileDialog1.FileName;
			this.saveFileDialog1.FileName = DateTime.Now.ToString("负荷计算");
			this.saveFileDialog1.Filter = "XLH文件(*.xlh)|*.xlh";
			if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
			{
				this.isEdit = false;
				this.dataGridView1.EndEdit();
				DataTable dgvToTable = this.GetDgvToTable(this.dataGridView1);
				DataTable dgvToTable2 = this.GetDgvToTable(this.dgv);
				DataTable dgvToTable3 = this.GetDgvToTable(this.dataGridView3);
				new DataSet
				{
					Tables = 
					{
						dgvToTable,
						dgvToTable2,
						dgvToTable3
					}
				}.WriteXml(this.saveFileDialog1.FileName);
				this.fullPath = this.saveFileDialog1.FileName;
				MessageBox.Show("数据成功保存到" + this.saveFileDialog1.FileName, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
		}

		private void tsbExportExcel_Click(object sender, EventArgs e)
		{
			this.dataGridView1.EndEdit();
			DataCalculationAndHelper.DataGridViewToExcel(new List<DataGridView>
			{
				this.dataGridView1,
				this.dgv,
				this.dataGridView3
			}, this);
		}

		private void tsbExportword_Click(object sender, EventArgs e)
		{
			this.dataGridView1.EndEdit();
			DataCalculationAndHelper.ExportDataGridView(new List<DataGridView>
			{
				this.dataGridView1,
				this.dgv,
				this.dataGridView3
			}, this);
		}

		private void tsbTransformSelect_Click(object sender, EventArgs e)
		{
			if (this.setNum % 2 == 0)
			{
				base.Size = new Size(764, 614);
				this.setNum++;
				return;
			}
			base.Size = new Size(764, 559);
			this.setNum++;
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.cmbSelectValue.DropDownStyle = ComboBoxStyle.DropDownList;
			this.cmbSelectValue.SelectedIndexChanged += this.cmbSelectValue_SelectedIndexChanged;
			this.cmbSelectValue.Visible = false;
			this.dataGridView1.Controls.Add(this.cmbSelectValue);
			this.loadDatagridView();
			base.Size = new Size(764, 559);
			this.stransformPowerData = DataCalculationAndHelper.GetTransformData(this.cmdData);
			if (this.stransformPowerData == null)
			{
				return;
			}
			foreach (TransformPowerData item in this.stransformPowerData)
			{
				this.cmbTransformType.Items.Add(item);
				this.cmbTransformType.DisplayMember = "transformName";
			}
			if (this.cmbTransformType.Items.Count > 0)
			{
				this.cmbTransformType.SelectedIndex = 0;
			}
			this.TotalCalculationForm(0);
			this.loadDatagridView();
		}

		private void cmbSelectValue_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.dataGridView1.CurrentCell.Value = ((System.Windows.Forms.ComboBox)sender).SelectedItem.ToString();
			((System.Windows.Forms.ComboBox)sender).Visible = false;
			if (this.cmbSelectValue.SelectedIndex != 3)
			{
				this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[3].Value = 0.22;
				return;
			}
			this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[3].Value = 0.38;
		}

		public void loadDatagridView()
		{
			this.dataGridView1.EndEdit();
			this.dataGridView1.Rows.Clear();
			this.dataGridView1.Rows.Add(2);
			this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].DefaultCellStyle.BackColor = this.color;
			this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].ReadOnly = true;
			this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[1].Value = "总计：";
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].DefaultCellStyle.BackColor = System.Drawing.Color.White;
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].ReadOnly = true;
			this.dataGridView1.Columns[7].ReadOnly = true;
			this.dataGridView1.Columns[7].DefaultCellStyle.BackColor = this.color;
			this.dataGridView1.Columns[8].ReadOnly = true;
			this.dataGridView1.Columns[8].DefaultCellStyle.BackColor = this.color;
			this.dataGridView1.Columns[9].ReadOnly = true;
			this.dataGridView1.Columns[9].DefaultCellStyle.BackColor = this.color;
			this.dataGridView1.Columns[10].ReadOnly = true;
			this.dataGridView1.Columns[10].DefaultCellStyle.BackColor = this.color;
			this.dgv.Rows.Clear();
			this.dgv.Rows.Add();
			this.dgv.Rows[0].Cells[0].Value = 0.9;
			this.dgv.Rows[0].Cells[1].Value = 0.97;
			this.dataGridView3.Rows.Clear();
			this.dataGridView3.Rows.Add();
			this.dataGridView3.Rows[0].Cells[0].Value = 0.7;
			this.dataGridView3.Rows[0].Cells[1].Value = 0.76;
			this.dataGridView3.Rows[0].Cells[2].Value = 0.9;
			this.dataGridView3.ClearSelection();
			for (int i = 0; i < this.dgv.Columns.Count; i++)
			{
				if (i >= 2)
				{
					this.dgv.Columns[i].ReadOnly = true;
					this.dgv.Columns[i].DefaultCellStyle.BackColor = this.color;
					this.dgv.Rows[0].Cells[i].Value = "";
				}
			}
			this.dataGridView3.Columns[3].ReadOnly = true;
			this.dataGridView3.Columns[3].DefaultCellStyle.BackColor = this.color;
			this.dataGridView3.Rows[0].Cells[3].Value = "";
			this.dataGridView3.Columns[5].ReadOnly = true;
			this.dataGridView3.Columns[5].DefaultCellStyle.BackColor = this.color;
			this.dataGridView3.Rows[0].Cells[5].Value = "";
			this.dataGridView3.Columns[6].ReadOnly = true;
			this.dataGridView3.Columns[6].DefaultCellStyle.BackColor = this.color;
			this.dataGridView3.Rows[0].Cells[6].Value = "";
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].DefaultCellStyle.BackColor = System.Drawing.Color.White;
			this.fullPath = string.Empty;
		}

		private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			if (e.RowIndex == this.dataGridView1.Rows.Count - 2)
			{
				try
				{
					using (Brush brush = new SolidBrush(System.Drawing.Color.Gray))
					{
						using (Brush brush2 = new SolidBrush(e.CellStyle.BackColor))
						{
							using (Pen pen = new Pen(brush))
							{
								e.Graphics.FillRectangle(brush2, e.CellBounds);
								if (e.RowIndex == this.dataGridView1.Rows.Count - 2 && e.ColumnIndex == 0 && e.Value != null)
								{
									e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, Brushes.Black, (float)(e.CellBounds.X + 2), (float)(e.CellBounds.Y + 2), StringFormat.GenericDefault);
								}
								if (e.RowIndex != this.dataGridView1.Rows.Count - 2)
								{
									e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
									if (e.Value != null)
									{
										e.Graphics.DrawString((string)e.Value, e.CellStyle.Font, Brushes.Black, (float)(e.CellBounds.X + 2), (float)(e.CellBounds.Y + 2), StringFormat.GenericDefault);
									}
								}
								else
								{
									e.Handled = true;
								}
								e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
							}
						}
					}
					if (e.RowIndex == this.dataGridView1.Rows.Count - 3)
					{
						e.Handled = true;
					}
					this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
				}
				catch (Exception)
				{
				}
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			if (this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[6].Value == null || this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[6].Value.ToString() == "" || this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[5].Value == null || this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[5].Value.ToString() == "")
			{
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
				this.TotalCalculationForm(e.RowIndex);
				return;
			}
			if ((e.ColumnIndex == 5 || e.ColumnIndex == 6 || e.ColumnIndex == 2 || e.ColumnIndex == 3) && this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[5].Value.ToString() == "1" && this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[6].Value.ToString() == "0.8" && this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[2].Value.ToString() == "A,B,C" && this.dataGridView1.Rows[this.dataGridView1.RowCount - 3].Cells[3].Value.ToString() == "0.38")
			{
				if (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)
				{
					this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "1";
				}
				if (e.RowIndex < this.dataGridView1.Rows.Count - 3)
				{
					this.TotalCalculationForm(e.RowIndex);
				}
				return;
			}
			if (this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null && !(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == ""))
			{
				if (e.RowIndex == this.dataGridView1.Rows.Count - 3)
				{
					this.isEdit = true;
					this.eidtorRow = true;
					this.dataGridView1.Rows.Add();
					this.AddrowSetDgv(this.dataGridView1.RowCount - 3);
				}
				else
				{
					this.isEdit = true;
				}
				this.TotalCalculationForm(e.RowIndex);
				return;
			}
			if (e.RowIndex == this.dataGridView1.Rows.Count - 3)
			{
				this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
				return;
			}
			this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
			this.TotalCalculationForm(e.RowIndex);
		}

		public void AddrowSetDgv(int index)
		{
			this.dataGridView1.Rows[index].ReadOnly = false;
			this.dataGridView1.Rows[index].Cells[7].Style.BackColor = this.color;
			this.dataGridView1.Rows[index].Cells[8].Style.BackColor = this.color;
			this.dataGridView1.Rows[index].Cells[9].Style.BackColor = this.color;
			this.dataGridView1.Rows[index].Cells[10].Style.BackColor = this.color;
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].DefaultCellStyle.BackColor = System.Drawing.Color.White;
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 2].ReadOnly = true;
		}

		public void calculationValue(int rowIndex)
		{
			double power = 0.0;
			if (this.dataGridView1.Rows[rowIndex].Cells[5].Value == null || this.dataGridView1.Rows[rowIndex].Cells[5].Value.ToString() == "")
			{
				return;
			}
			if (this.dataGridView1.Rows[rowIndex].Cells[6].Value == null || this.dataGridView1.Rows[rowIndex].Cells[6].Value.ToString() == "")
			{
				return;
			}
			if (this.dataGridView1.Rows[rowIndex].Cells[4].Value != null && this.dataGridView1.Rows[rowIndex].Cells[4].Value.ToString() != "")
			{
				power = Convert.ToDouble(this.dataGridView1.Rows[rowIndex].Cells[4].Value);
			}
			double num = DataCalculationAndHelper.ActivePower(power, Convert.ToDouble(this.dataGridView1.Rows[rowIndex].Cells[5].Value));
			double num2 = DataCalculationAndHelper.RectivePower(num, Convert.ToDouble(this.dataGridView1.Rows[rowIndex].Cells[6].Value));
			double var = DataCalculationAndHelper.ViewPower(num, num2);
			double var2;
			if (this.dataGridView1.Rows[rowIndex].Cells[2].Value.ToString() == "A,B,C")
			{
				var2 = DataCalculationAndHelper.EleCurrentThree(var);
			}
			else
			{
				var2 = DataCalculationAndHelper.EleCurrentOne(var);
			}
			this.dataGridView1.Rows[rowIndex].Cells[7].Value = DataCalculationAndHelper.MathDecimal(num, 2);
			this.dataGridView1.Rows[rowIndex].Cells[8].Value = DataCalculationAndHelper.MathDecimal(num2, 2);
			this.dataGridView1.Rows[rowIndex].Cells[9].Value = DataCalculationAndHelper.MathDecimal(var, 2);
			this.dataGridView1.Rows[rowIndex].Cells[10].Value = DataCalculationAndHelper.MathDecimal(var2, 2);
			DataGridViewRow dataGridViewRow = this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1];
			double num3 = this.SumData(this.dataGridView1, 7);
			double num4 = this.SumData(this.dataGridView1, 8);
			double var3 = DataCalculationAndHelper.ViewPower(num3, num4);
			double var4 = DataCalculationAndHelper.EleCurrentThree(var3);
			dataGridViewRow.Cells[4].Value = this.SumColumnsVale(this.dataGridView1, 4);
			dataGridViewRow.Cells[7].Value = DataCalculationAndHelper.MathDecimal(num3, 2);
			dataGridViewRow.Cells[8].Value = DataCalculationAndHelper.MathDecimal(num4, 2);
			dataGridViewRow.Cells[9].Value = DataCalculationAndHelper.MathDecimal(var3, 2);
			dataGridViewRow.Cells[10].Value = DataCalculationAndHelper.MathDecimal(var4, 2);
		}

		public double SumData(DataGridView dgv, int columnIndex)
		{
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			double num4 = 0.0;
			for (int i = 0; i < dgv.Rows.Count - 2; i++)
			{
				if (this.dataGridView1.Rows[i].Cells[columnIndex].Value != null && this.dataGridView1.Rows[i].Cells[columnIndex].Value.ToString() != "")
				{
					if (this.dataGridView1.Rows[i].Cells[2].Value.ToString() == "A")
					{
						num += Convert.ToDouble(this.dataGridView1.Rows[i].Cells[columnIndex].Value);
					}
					else if (this.dataGridView1.Rows[i].Cells[2].Value.ToString() == "B")
					{
						num2 += Convert.ToDouble(this.dataGridView1.Rows[i].Cells[columnIndex].Value);
					}
					else if (this.dataGridView1.Rows[i].Cells[2].Value.ToString() == "C")
					{
						num3 += Convert.ToDouble(this.dataGridView1.Rows[i].Cells[columnIndex].Value);
					}
					else
					{
						num4 += Convert.ToDouble(this.dataGridView1.Rows[i].Cells[columnIndex].Value);
					}
				}
			}
			double num5;
			if (num >= num2)
			{
				num5 = num;
			}
			else
			{
				num5 = num2;
			}
			if (num5 <= num3)
			{
				num5 = num3;
			}
			return num5 * 3.0 + num4;
		}

		public double SumColumnsVale(DataGridView dgv, int columnIndex)
		{
			double num = 0.0;
			for (int i = 0; i < dgv.Rows.Count - 2; i++)
			{
				if (this.dataGridView1.Rows[i].Cells[columnIndex].Value != null && this.dataGridView1.Rows[i].Cells[columnIndex].Value.ToString() != "")
				{
					num += Convert.ToDouble(this.dataGridView1.Rows[i].Cells[columnIndex].Value);
				}
			}
			return num;
		}

		public string AVGColumnsValue(DataGridView dgv, int columnIndex)
		{
			double num = 0.0;
			int num2 = 0;
			for (int i = 0; i < dgv.Rows.Count - 2; i++)
			{
				if (this.dataGridView1.Rows[i].Cells[columnIndex].Value != null && this.dataGridView1.Rows[i].Cells[columnIndex].Value.ToString() != "")
				{
					num += Convert.ToDouble(this.dataGridView1.Rows[i].Cells[columnIndex].Value);
				}
				num2++;
			}
			if (num2 > 0)
			{
				return (num / (double)num2).ToString("0.00");
			}
			return num.ToString();
		}

		private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			this.dataGridView1.Rows[e.RowIndex].Cells[5].Value = 1.0;
			this.dataGridView1.Rows[e.RowIndex].Cells[6].Value = 0.8;
			this.dataGridView1.Rows[e.RowIndex].Cells[2].Value = "A,B,C";
			this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = 0.38;
			if (e.RowIndex > 1 && this.eidtorRow)
			{
				this.dataGridView1.Rows[e.RowIndex - 1].Cells[5].Value = 1.0;
				this.dataGridView1.Rows[e.RowIndex - 1].Cells[6].Value = 0.8;
				this.dataGridView1.Rows[e.RowIndex - 1].Cells[2].Value = "A,B,C";
				this.dataGridView1.Rows[e.RowIndex - 1].Cells[3].Value = 0.38;
			}
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Cells[5].Value = "";
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Cells[6].Value = "";
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Cells[2].Value = "";
			this.dataGridView1.Rows[this.dataGridView1.RowCount - 1].Cells[3].Value = "";
		}

		private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (e.Control is DataGridViewTextBoxEditingControl)
			{
				this.tb = (DataGridViewTextBoxEditingControl)e.Control;
				if (0 <= this.dataGridView1.CurrentCell.RowIndex && this.dataGridView1.CurrentCell.RowIndex < this.dataGridView1.RowCount - 3)
				{
					this.tb.ContextMenuStrip = this.contextMenuStrip1;
					this.cmcopy.Enabled = true;
					this.cmCut.Enabled = true;
					this.cmdelete.Enabled = true;
				}
				else if (this.dataGridView1.CurrentCell.RowIndex == this.dataGridView1.RowCount - 3)
				{
					this.tb.ContextMenuStrip = this.contextMenuStrip1;
					this.cmcopy.Enabled = false;
					this.cmCut.Enabled = false;
					this.cmdelete.Enabled = false;
				}
				else
				{
					this.tb.ContextMenuStrip = null;
				}
				this.dgvTxt = this.tb;
				if (this.dataGridView1.CurrentCell.ColumnIndex > 1)
				{
					this.tb.KeyPress += this.Cells_KeyPress;
				}
			}
		}

		private void Cells_KeyPress(object sender, KeyPressEventArgs e)
		{
			DataCalculationAndHelper.keyPressXS(e, this.dgvTxt, this.dataGridView1.CurrentCell.ColumnIndex);
		}

		private void data_cellPress(object sender, KeyPressEventArgs e)
		{
			DataCalculationAndHelper.keyPressXS(e, this.dgvTxt, -1);
		}

		private void dgv_cellPress(object sender, KeyPressEventArgs e)
		{
			DataCalculationAndHelper.keyPressXS(e, this.dgvTxt, -1);
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dataGridView1.BeginEdit(true);
			}
			this.cmbSelectValue.Items.Clear();
			if (this.dataGridView1.CurrentCell.ColumnIndex == 2 && e.RowIndex < this.dataGridView1.RowCount - 2)
			{
				this.AddComboxitem();
				if (this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value != null)
				{
					this.cmbSelectValue.Text = this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Value.ToString();
				}
				if (this.dataGridView1.CurrentCell.Value != null)
				{
					this.cmbSelectValue.Text = this.dataGridView1.CurrentCell.Value.ToString();
				}
				System.Drawing.Rectangle cellDisplayRectangle = this.dataGridView1.GetCellDisplayRectangle(this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex, false);
				this.cmbSelectValue.Left = cellDisplayRectangle.Left;
				this.cmbSelectValue.Top = cellDisplayRectangle.Top;
				this.cmbSelectValue.Width = cellDisplayRectangle.Width;
				this.cmbSelectValue.Height = cellDisplayRectangle.Height;
				this.cmbSelectValue.Visible = true;
				return;
			}
			this.cmbSelectValue.Visible = false;
		}

		private void AddComboxitem()
		{
			this.cmbSelectValue.Items.Add("A");
			this.cmbSelectValue.Items.Add("B");
			this.cmbSelectValue.Items.Add("C");
			this.cmbSelectValue.Items.Add("A,B,C");
		}

		private void cmdelete_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.dataGridView1.Rows.Count > 3 && this.dataGridView1.CurrentCell.RowIndex != this.dataGridView1.Rows.Count - 2)
				{
					this.dataGridView1.Rows.Remove(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex]);
				}
			}
			catch (Exception)
			{
			}
			this.dataGridView1.Refresh();
			this.TotalCalculationForm(0);
		}

		private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dgv.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dgv.BeginEdit(true);
			}
		}

		private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
			{
				this.dataGridView3.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
				this.dataGridView3.BeginEdit(true);
			}
		}

		private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (e.Control is DataGridViewTextBoxEditingControl)
			{
				DataGridViewTextBoxEditingControl dataGridViewTextBoxEditingControl = (DataGridViewTextBoxEditingControl)e.Control;
				this.dgvTxt = dataGridViewTextBoxEditingControl;
				dataGridViewTextBoxEditingControl.KeyPress += this.data_cellPress;
			}
		}

		private void dataGridView3_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (e.Control is DataGridViewTextBoxEditingControl)
			{
				DataGridViewTextBoxEditingControl dataGridViewTextBoxEditingControl = (DataGridViewTextBoxEditingControl)e.Control;
				this.dgvTxt = dataGridViewTextBoxEditingControl;
				dataGridViewTextBoxEditingControl.KeyPress += this.dgv_cellPress;
			}
		}

		private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			this.TotalCalculationForm(0);
		}

		public void DgvCalculation()
		{
			this.CalActPower();
			this.CalReActPower();
			double var = DataCalculationAndHelper.ViewPower(Convert.ToDouble(this.dgv.Rows[0].Cells[3].Value), Convert.ToDouble(this.dgv.Rows[0].Cells[4].Value));
			this.dgv.Rows[0].Cells[5].Value = DataCalculationAndHelper.MathDecimal(var, 2);
			double var2 = DataCalculationAndHelper.TotolPowerFactor(Convert.ToDouble(this.dgv.Rows[0].Cells[4].Value), Convert.ToDouble(this.dgv.Rows[0].Cells[3].Value));
			if (var2.ToString() == "非数字")
			{
				this.dgv.Rows[0].Cells[2].Value = 0;
			}
			else
			{
				this.dgv.Rows[0].Cells[2].Value = DataCalculationAndHelper.MathDecimal(var2, 2);
			}
			double var3 = DataCalculationAndHelper.EleCurrentThree(var);
			this.dgv.Rows[0].Cells[6].Value = DataCalculationAndHelper.MathDecimal(var3, 2);
		}

		public double CalActPower()
		{
			double num = Convert.ToDouble(this.dgv.Rows[0].Cells[0].Value) * Convert.ToDouble(this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[7].Value);
			this.dgv.Rows[0].Cells[3].Value = DataCalculationAndHelper.MathDecimal(num, 2);
			return num;
		}

		public void CalReActPower()
		{
			double var = Convert.ToDouble(this.dgv.Rows[0].Cells[1].Value) * Convert.ToDouble(this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[8].Value);
			this.dgv.Rows[0].Cells[4].Value = DataCalculationAndHelper.MathDecimal(var, 2);
		}

		public double tanValueBycos(double db)
		{
			return Math.Tan(Math.Acos(db));
		}

		private void cmcopy_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.RowCount > 3 && this.dataGridView1.CurrentCell.RowIndex < this.dataGridView1.RowCount - 3)
			{
				this.copyDgvRowValue();
			}
		}

		private void cmpast_Click(object sender, EventArgs e)
		{
			this.eidtorRow = false;
			int num;
			if (this.dataGridView1.CurrentCell.RowIndex >= this.dataGridView1.RowCount - 3)
			{
				num = this.dataGridView1.RowCount - 3;
			}
			else
			{
				num = this.dataGridView1.CurrentCell.RowIndex + 1;
			}
			this.dataGridView1.Rows.Insert(num, new DataGridViewRow());
			for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
			{
				this.dataGridView1.Rows[num].Cells[i].Value = this.DgvRowValue[i];
			}
			this.AddrowSetDgv(num);
			this.TotalCalculationForm(num);
		}

		private void cmCut_Click(object sender, EventArgs e)
		{
			if (this.dataGridView1.RowCount > 3 && this.dataGridView1.CurrentCell.RowIndex < this.dataGridView1.RowCount - 3)
			{
				this.copyDgvRowValue();
				this.dataGridView1.Rows.Remove(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex]);
				this.TotalCalculationForm(this.dataGridView1.CurrentCell.RowIndex);
			}
		}

		public void copyDgvRowValue()
		{
			this.DgvRowValue.Clear();
			for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
			{
				if (this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[i].Value == null)
				{
					this.DgvRowValue.Add("");
				}
				else
				{
					this.DgvRowValue.Add(this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex].Cells[i].Value.ToString());
				}
			}
			this.cmpast.Enabled = true;
		}

		private void dataGridView3_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			this.TotalCalculationForm(0);
		}

		public void Datagridview3Calculation()
		{
			this.ComPasaTionCapaCity();
			if (this.dgv.Rows[0].Cells[4].Value != null)
			{
				double num = 0.0;
				double activePower = Convert.ToDouble(this.dgv.Rows[0].Cells[3].Value);
				double num2 = Convert.ToDouble(this.dgv.Rows[0].Cells[4].Value);
				if (this.dataGridView3.Rows[0].Cells[4].Value != null && this.dataGridView3.Rows[0].Cells[4].Value.ToString() != "")
				{
					num = Convert.ToDouble(this.dataGridView3.Rows[0].Cells[4].Value);
				}
				double num3;
				if (this.dataGridView3.Rows[0].Cells[4].Value == null || this.dataGridView3.Rows[0].Cells[4].Value.ToString() == "")
				{
					num3 = num2 - Convert.ToDouble(this.dataGridView3.Rows[0].Cells[3].Value);
				}
				else
				{
					num3 = num2 - num;
				}
				if (num3.ToString() == "非数字")
				{
					this.dataGridView3.Rows[0].Cells[5].Value = 0;
				}
				else
				{
					this.dataGridView3.Rows[0].Cells[5].Value = DataCalculationAndHelper.MathDecimal(num3, 2);
				}
				if (DataCalculationAndHelper.ViewPower(activePower, num3).ToString() == "非数字")
				{
					this.dataGridView3.Rows[0].Cells[6].Value = 0;
					return;
				}
				this.dataGridView3.Rows[0].Cells[6].Value = DataCalculationAndHelper.MathDecimal(DataCalculationAndHelper.ViewPower(activePower, num3), 2);
			}
		}

		public void ComPasaTionCapaCity()
		{
			double activePower = Convert.ToDouble(this.dgv.Rows[0].Cells[3].Value);
			double reActivePower = Convert.ToDouble(this.dgv.Rows[0].Cells[4].Value);
			double num = Convert.ToDouble(this.dataGridView3.Rows[0].Cells[0].Value);
			double reactiveLoadFactor = Convert.ToDouble(this.dataGridView3.Rows[0].Cells[1].Value);
			double cos = DataCalculationAndHelper.CalculatComFator(activePower, reActivePower, num, reactiveLoadFactor);
			double var = DataCalculationAndHelper.CompasationCapacity(num, activePower, DataCalculationAndHelper.cosTransformTan(cos), DataCalculationAndHelper.cosTransformTan(Convert.ToDouble(this.dataGridView3.Rows[0].Cells[2].Value)));
			if (var.ToString() == "非数字")
			{
				this.dataGridView3.Rows[0].Cells[3].Value = 0;
				return;
			}
			this.dataGridView3.Rows[0].Cells[3].Value = DataCalculationAndHelper.MathDecimal(var, 2);
		}

		private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
		{
		}

		private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.tbSetNum(sender, e);
		}

		private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.tbSetNum(sender, e);
		}

		public void tbSetNum(object sender, KeyPressEventArgs e)
		{
			if (char.IsControl(e.KeyChar))
			{
				return;
			}
			if (char.IsDigit(e.KeyChar) && (e.KeyChar & 'ÿ') == e.KeyChar)
			{
				return;
			}
			if (e.KeyChar == '.' && ((System.Windows.Forms.TextBox)sender).Text.Split(new char[]
			{
				'.'
			}).Length < 2)
			{
				return;
			}
			e.Handled = true;
		}

		public void CalculatTranformPower()
		{
			if (this.dataGridView3.Rows.Count == 0)
			{
				return;
			}
			double num = 0.0;
			if (this.dataGridView3.Rows[0].Cells[6].Value != null && this.dataGridView3.Rows[0].Cells[6].Value.ToString() != null)
			{
				num = Convert.ToDouble(this.dataGridView3.Rows[0].Cells[6].Value);
			}
			double num2 = num / Convert.ToDouble(this.tbLoadFactor.Text);
			if (this.numCount.Value > 0m)
			{
				num2 /= Convert.ToDouble(this.numCount.Value);
			}
			num2 = Math.Round(num2, 2);
			new List<double>();
			TransformPowerData transformPowerData = (TransformPowerData)this.cmbTransformType.SelectedItem;
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			if (this.stransformPowerData == null)
			{
				return;
			}
			foreach (TransformPowerData transformPowerData2 in this.stransformPowerData)
			{
				if (transformPowerData2.TransformName == transformPowerData.TransformName)
				{
					dictionary = transformPowerData2.TransformDic;
					break;
				}
			}
			bool flag = true;
			dictionary = (from entry in dictionary
			orderby entry.Value
			select entry).ToDictionary((KeyValuePair<string, double> pair) => pair.Key, (KeyValuePair<string, double> pair) => pair.Value);
			foreach (KeyValuePair<string, double> keyValuePair in dictionary)
			{
				if (keyValuePair.Value / 1000.0 >= num2)
				{
					this.cmbTranPower.Text = keyValuePair.Key;
					flag = false;
					break;
				}
			}
			if (flag && this.cmbTranPower.Items.Count > 0)
			{
				this.cmbTranPower.SelectedIndex = this.cmbTranPower.Items.Count - 1;
			}
		}

		private void cmbTransformType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.cmbTranPower.Items.Clear();
			foreach (TransformPowerData transformPowerData in this.stransformPowerData)
			{
				TransformPowerData transformPowerData2 = (TransformPowerData)this.cmbTransformType.SelectedItem;
				if (transformPowerData.TransformName == transformPowerData2.TransformName)
				{
					foreach (KeyValuePair<string, double> keyValuePair in transformPowerData.TransformDic)
					{
						this.cmbTranPower.Items.Add(keyValuePair.Key);
					}
				}
			}
			if (this.cmbTranPower.Items.Count > 0)
			{
				this.cmbTranPower.SelectedIndex = 0;
			}
			this.TotalCalculationForm(0);
		}

		public void TotalCalculationForm(int index)
		{
			this.calculationValue(index);
			this.DgvCalculation();
			this.Datagridview3Calculation();
			this.CalculatTranformPower();
		}

		private void tbLoadFactor_TextChanged(object sender, EventArgs e)
		{
			this.TotalCalculationForm(0);
		}

		private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right && e.RowIndex < this.dataGridView1.RowCount - 2)
			{
				if (e.RowIndex == this.dataGridView1.RowCount - 3)
				{
					this.cmcopy.Enabled = false;
					this.cmCut.Enabled = false;
					this.cmdelete.Enabled = false;
				}
				else
				{
					this.cmcopy.Enabled = true;
					this.cmCut.Enabled = true;
					this.cmdelete.Enabled = true;
				}
				this.dataGridView1.PointToScreen(new System.Drawing.Point(0, 0));
				int num = 0;
				int num2 = 0;
				foreach (object obj in this.dataGridView1.Columns)
				{
					DataGridViewColumn dataGridViewColumn = (DataGridViewColumn)obj;
					if (dataGridViewColumn.Index >= e.ColumnIndex)
					{
						break;
					}
					if (dataGridViewColumn.Visible)
					{
						num += dataGridViewColumn.Width;
					}
				}
				foreach (object obj2 in ((IEnumerable)this.dataGridView1.Rows))
				{
					DataGridViewRow dataGridViewRow = (DataGridViewRow)obj2;
					if (dataGridViewRow.Index > e.RowIndex)
					{
						break;
					}
					if (dataGridViewRow.Visible)
					{
						num2 += dataGridViewRow.Height;
					}
				}
				if (e.RowIndex == this.dataGridView1.CurrentCell.RowIndex)
				{
					this.contextMenuStrip1.Show(this.dataGridView1.PointToScreen(new System.Drawing.Point(num + e.X, num2 + e.Y)));
				}
			}
		}

		private void dataGridView1_SelectionChanged(object sender, EventArgs e)
		{
		}

		private void numCount_ValueChanged(object sender, EventArgs e)
		{
			this.TotalCalculationForm(0);
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			if (this.fullPath == null || this.fullPath == "")
			{
				this.SaveXml();
			}
			else
			{
				this.dataGridView1.EndEdit();
				DataTable dgvToTable = this.GetDgvToTable(this.dataGridView1);
				DataTable dgvToTable2 = this.GetDgvToTable(this.dgv);
				DataTable dgvToTable3 = this.GetDgvToTable(this.dataGridView3);
				new DataSet
				{
					Tables = 
					{
						dgvToTable,
						dgvToTable2,
						dgvToTable3
					}
				}.WriteXml(this.fullPath);
				MessageBox.Show("数据成功保存到" + this.fullPath, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			}
			this.isEdit = false;
		}

		private const string Phase_A = "A";

		private const string Phase_B = "B";

		private const string Phase_C = "C";

		private const string Phase_3P = "A,B,C";

		private Document ui_doc;

		private System.Windows.Forms.ComboBox cmbSelectValue = new System.Windows.Forms.ComboBox();

		private ExternalCommandData cmdData;

		private List<string> DgvRowValue = new List<string>();

		private List<TransformPowerData> stransformPowerData;

		private bool isEdit;

		private int setNum;

		private string fullPath;

		private bool eidtorRow;

		private System.Drawing.Color color = System.Drawing.Color.Wheat;

		public DataGridViewTextBoxEditingControl dgvTxt;

		private DataGridViewTextBoxEditingControl tb;
	}
}
